using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Drawing;

using SBPweb.Modules.Modularity;
using SBPweb.Modules.Smartparts;

namespace SBPweb.Presentation.Windows.WPF.Controls
{
    public partial class DockingUserControl : UserControl, IDockingSmartpart
    {
        #region Private fields

        private bool pIsModal;
        private bool mIsWindow = true;
        private string pText = string.Empty;
        private object DockControl;
        private Point pFloatingLocation;
        private Size pFloatingSize;
        private Size pFloatingMinimumSize;
        private IModule pParentModule;
        private AppearanceStyle pAppearanceStyle = AppearanceStyle.Tabbed;
        private BorderStyle mBorderStyle = SBPweb.Modules.Smartparts.BorderStyle.SizableToolWindow;

        #endregion

        #region Constructor logic

        public DockingUserControl()
        {
            mInitialize();
        }

        public DockingUserControl(IModule parentModule)
        {
            pParentModule = parentModule;
            mInitialize();
        }

        private void mInitialize()
        {
            BorderBrush = System.Windows.Media.Brushes.White;
            BorderThickness = new System.Windows.Thickness(1);
            /*
            TextChanged += new EventHandler(DockingUserControl_TextChanged);

            DockControl.DockSituationChanged += new EventHandler(pDockControl_DockSituationChanged);
            DockControl.Closed += new EventHandler(DockControl_Closed);

            HideOnlyWhenClose = true;

            DockControl.Size = Size;
            DockControl.FloatingSize = Size;

            DockControl.Controls.Add(this);
            if (ParentModule != null && ParentModule.Icon != null)
            {
                DockControl.TabImage = ParentModule.Icon.ToBitmap().GetThumbnailImage(16, 16, null, IntPtr.Zero);
            }

            DockControl.PersistState = true;

            if (string.IsNullOrEmpty(Name))
            {
                Name = ToString();
            }

            DockControl.Name = "dc" + Name;
            */
        }

        #endregion

        #region Property accessors

        public object DockControlInstance
        {
            get
            {
                return DockControl;
            }
        }

        public IModule ParentModule
        {
            get
            {
                return pParentModule;
            }
        }

        public new bool IsVisible
        {
            get
            {
                return base.IsVisible;
            }
            set
            {
                base.Visibility = (value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden);
                /*
                if ((DockControl.IsTabbedDocument || DockControl.IsDocked || DockControl.IsFloating) != IsVisible && DockControl.Manager != null )
                {
                    if (IsVisible)
                    {
                        if (InitialAppearanceStyle == AppearanceStyle.Float)
                        {
                            DockControl.OpenFloating(WindowOpenMethod.OnScreenActivate);
                        }
                        else
                        {
                            DockControl.Open(WindowOpenMethod.OnScreenActivate);
                        }
                    }
                    else
                    {
                        DockControl.Close();
                    }
                }
                */
                if (this.VisibleChanged != null)
                {
                    this.VisibleChanged.Invoke(this, EventArgs.Empty);
                }
            }
        }

        public new bool IsEnabled
        {
            get
            {
                return base.IsEnabled;
            }
            set
            {
                base.IsEnabled = value;
                if (this.EnabledChanged != null)
                {
                    this.EnabledChanged.Invoke(this, EventArgs.Empty);
                }
            }
        }

        public bool IsModal
        {
            get
            {
                return pIsModal;
            }
            protected set
            {
                pIsModal = value;
            }
        }

        public AppearanceStyle InitialAppearanceStyle
        {
            get
            {
                return pAppearanceStyle;
            }
            set
            {
                if (value == AppearanceStyle.Unknown)
                {
                    pAppearanceStyle = AppearanceStyle.Tabbed;
                }
                else
                {
                    pAppearanceStyle = value;
                }
			}
        }

        public AppearanceStyle CurrentAppearanceStyle
        {
            get
            {
                /*
                if (DockControl.IsDocked) return AppearanceStyle.Docked;
                if (DockControl.IsTabbedDocument) return AppearanceStyle.Tabbed;
                if (DockControl.IsFloating) return AppearanceStyle.Float;
                */
                return AppearanceStyle.Unknown;
            }
        }

        public string Caption
        {
            get
            {
                return pText;
            }
            set
            {
                pText = value;
            }
        }

        public Size FloatingMinimumSize
        {
            get
            {
                return pFloatingMinimumSize;
            }
            set
            {
                pFloatingMinimumSize = value;
            }
        }

		public bool HideOnlyWhenClose
		{
			get
			{
                throw new NotImplementedException();
			}
			set
			{
                throw new NotImplementedException();
            }
		}

        public bool IsShowInWindowMenu
        {
            get
            {
                return mIsWindow;
            }
            set
            {
                mIsWindow = value;
            }
        }

        public BorderStyle BorderStyle
        {
            get
            {
                return mBorderStyle;
            }
            set
            {
                mBorderStyle = value;
                /*
                switch (value)
                {
                    case SBPweb.Modules.Smartparts.BorderStyle.Fixed3D:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.RaisedThick;
                        break;
                    case SBPweb.Modules.Smartparts.BorderStyle.FixedDialog:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.RaisedThick;
                        break;
                    case SBPweb.Modules.Smartparts.BorderStyle.FixedSingle:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.RaisedThin;
                        break;
                    case SBPweb.Modules.Smartparts.BorderStyle.FixedToolWindow:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.Flat;
                        break;
                    case SBPweb.Modules.Smartparts.BorderStyle.None:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.None;
                        break;
                    case SBPweb.Modules.Smartparts.BorderStyle.Sizable:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.RaisedThick;
                        break;
                    case SBPweb.Modules.Smartparts.BorderStyle.SizableToolWindow:
                        pDockControl.BorderStyle = TD.SandDock.Rendering.BorderStyle.RaisedThin;
                        break;
                }
                */
            }
        }

        public bool AllowDockCenter
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool AllowDockLeft
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool AllowDockRight
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool AllowDockTop
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool AllowFloat
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool Collapsed
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int DockedSize
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Point FloatingLocation
        {
            get
            {
                return pFloatingLocation;
            }
            set
            {
                pFloatingLocation = value;
            }
        }

        public Size FloatingSize
        {
            get
            {
                if (pFloatingSize.IsEmpty)
                {
                    pFloatingSize.Width = 1;
                    pFloatingSize.Height = 1;
                }
                return pFloatingSize;
            }
            set
            {
                pFloatingSize = value;
            }
        }

        public bool AllowClose
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool AllowCollapse
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool AllowDockBottom
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        
        #endregion

        #region Event handlers

        /*
        void DockControl_Closed(object sender, EventArgs e)
        {
            base.Visible = false;
        }

        void DockingUserControl_TextChanged(object sender, EventArgs e)
        {
            DockControl.TabText = DockControl.Text = Text;
        }

        void pDockControl_DockSituationChanged(object sender, EventArgs e)
        {
            if (sender != DockControl) return;

            if (DockControl.IsTabbedDocument || DockControl.IsDocked || DockControl.IsFloating)
            {
                foreach (System.Windows.Forms.Control c in DockControl.Controls)
                {
                    c.Visible = true;
                }
            }

            if (DockControl != null && DockControl.Parent != null && DockControl.Parent.Parent != null)
            {
                Form form = DockControl.Parent.Parent as Form;
                if (form != null)
                {
                    form.MinimumSize = FloatingMinimumSize;
                }
            }
        }
        */

        public event EventHandler VisibleChanged;

        public event EventHandler EnabledChanged;

        #endregion

    }

}
